﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;

namespace Aprendete.Models
{
    public partial class Usuario
    {

        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(Nombre))
                yield return new RuleViolation("Nombre requerido", "Nombre");

            if (String.IsNullOrEmpty(Apellido))
                yield return new RuleViolation("Apellido requerido", "Apellido");

            if (String.IsNullOrEmpty(Rut))
                yield return new RuleViolation("Rut requerido", "Rut");
            
            if (!Utilitarios_Joseto.RutCorrecto(Rut))
                yield return new RuleViolation("Rut inexistente, por favor corrijalo", "Rut");

            if (String.IsNullOrEmpty(Colegio))
                yield return new RuleViolation("Colegio requerido", "Colegio");

            if (String.IsNullOrEmpty(Curso))
                yield return new RuleViolation("Curso requerido", "Curso");

            if (String.IsNullOrEmpty(EsProfesor.ToString()))
                yield return new RuleViolation("Debes seleccionar si deseas ser, o no profesor.", "Ser Profesor");

            if (String.IsNullOrEmpty(Mail))
                yield return new RuleViolation("Mail requerido", "Mail");

            if (String.IsNullOrEmpty(Nacimiento.ToString()))
                yield return new RuleViolation("Fecha de nacimiento requerida", "Fecha de Nacimiento");

            yield break;
        }

        partial void OnValidate(ChangeAction action)
        {
            if (!IsValid)
                throw new ApplicationException("Rule violations prevent saving");
        }
    }

    public class RuleViolation
    {

        public string ErrorMessage { get; private set; }
        public string PropertyName { get; private set; }

        public RuleViolation(string errorMessage)
        {
            ErrorMessage = errorMessage;
        }

        public RuleViolation(string errorMessage, string propertyName)
        {
            ErrorMessage = errorMessage;
            PropertyName = propertyName;
        }
    } 
}